<template>
  <div class="container">
    <div class="btnList">
      <el-date-picker
        v-model="erpCenter.date"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-check"
        @click="pull()"
        >拉取差异单</el-button
      >
    </div>
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.code"
        placeholder="请输入单号"
        clearable
      ></el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="serial_code"
        label="差异单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sheet_no"
        label="ERP差异单号"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        prop="branch_no"
        label="发货仓库编码"
        align="center"
      ></el-table-column> -->
      <el-table-column
        prop="branch_name"
        label="发货仓库名称"
        align="center"
      ></el-table-column>
      <!-- <el-table-column
        prop="d_branch_no"
        label="目标仓库编码"
        align="center"
      ></el-table-column> -->
      <el-table-column
        prop="d_branch_name"
        label="目标仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="voucher_no"
        label="原始ERP单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="bar_code"
        label="货号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="品名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="real_qty"
        label="数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="large_qty"
        label="箱数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="valid_price"
        label="单价"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount"
        label="总金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="oper_date"
        label="日期"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="other3"
        label="备注"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
  </div>
</template>
<script>
import { parseTime, getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";

import { getToken } from "@/utils/auth";

import {
  pullDifference,
  selectDifferenceList,
} from "@/api/difference/differenceList";
export default {
  components: {
    pagination: Pagination,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //默认时间
      normal_startime: getnowDate().startimer,
      normal_endtime: getnowDate().endtimer,
      search: {
        code: "",
        start_time: "",
        end_time: "",
      },
      erpCenter: {
        date: new Date(),
      },
      date: [],
    };
  },

  created() {
    this.search.start_time = this.normal_startime;
    this.search.end_time = this.normal_endtime;
    if (this.normal_startime) {
      this.date.push(this.search.start_time);
    }
    if (this.normal_endtime) {
      this.date.push(this.search.end_time);
    }
    //页面数据
    this.getData();
    //取初始商品列表
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        code: this.search.code,
        start_date: this.search.start_time,
        end_date: this.search.end_time,
      };
      selectDifferenceList(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 本地搜索
    handleSearch() {
      if (this.date) {
        this.search.start_time = this.date[0];
        this.search.end_time = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },

    //清空
    empty() {
      this.date = [];
      this.date.push(this.normal_startime);
      this.date.push(this.normal_endtime);
      this.search = {
        code: "",
        start_time: this.normal_startime,
        end_time: this.normal_endtime,
      };
      this.erpCenter = {
        date: new Date(),
      };
    },

    //拉取erp
    pull(scope) {
      let data = {
        pull_date: parseTime(this.erpCenter.date).split(" ")[0],
      };
      this.$confirm("是否拉取ERP?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          pullDifference(data)
            .then((res) => {
              if (res.code == 0) {
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消拉取ERP",
          });
        });
    },
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Difference/exportDifference?token=" +
            getToken() +
            "&code=" +
            this.search.code +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>